<template>
  <el-dialog
    append-to-body
    :close-on-click-modal="false"
    :visible.sync="dialogFormVisible"
    :before-close="close"
    :title="title"
    width="800px"
    :top="'10vh'"
  >
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item
            label="任务名称"
            prop="name"
            :rules="[{ required: true, message: '请输入任务名称', trigger: 'blur' }]"
            style="width: 100%"
          >
            <el-input v-model="form.name" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="所属任务组"
            prop="taskCategory"
            :rules="[{ required: true, message: '请选择任务组', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-select v-model="form.taskCategory" placeholder="请选择任务组">
              <el-option
                v-for="item in dict.task_group"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="数据来源"
            prop="queryType"
            :rules="[{ required: true, message: '请选择数据来源', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-select v-model="form.queryType" placeholder="请选择数据来源">
              <el-option value="sql" label="数据库查询" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="任务类型"
            prop="dataType"
            :rules="[{ required: true, message: '请选择任务类型', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-select v-model="form.dataType" placeholder="请选择任务类型">
              <el-option
                v-for="item in dict.task_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="执行内容"
            prop="dataId"
            :rules="[{ required: true, message: '请选择执行内容', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-select v-model="form.dataId" placeholder="请选择执行内容">
              <el-option
                v-for="item in dataSources"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="定时策略"
            prop="corn"
            :rules="[{ required: true, message: '请输入定时策略', trigger: 'blur' }]"
            style="width: 100%"
          >
            <el-popover v-model="cronPopover">
              <cron i18n="cn" @change="changeCron" @close="cronPopover=false" />
              <el-input slot="reference" v-model="form.corn" placeholder="请输入定时策略" @click="cronPopover=true" />
            </el-popover>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { saveObject, updateObject } from '@/api/quartz/task/index'
import { queryAllLayz } from '@/api/quartz/data/index'
import { cron } from 'vue-cron'
export default {
  name: 'TaskEntity',
  dicts: ['task_type', 'task_group'],
  components: { cron },
  data() {
    return {
      title: '数据项管理',
      loading: false,
      testLoading: false,
      dialogFormVisible: false,
      dataSources: [],
      form: { id: null, name: '', taskName: '', taskCategory: '', queryType: 'sql',
        state: 'NORMAL', dataId: null, taskClass: '', corn: '', dataType: '', jxx: '' },
      queryData: null,
      cronPopover: false
    }
  },
  methods: {
    openEntity(data) {
      this.queryData = null
      this.initDataSource()
      if (undefined !== data && undefined !== data.id && data.id !== null) {
        this.form = Object.assign({}, data)
        this.title = '修改数据项信息'
        this.dialogFormVisible = true
      } else {
        this.title = '新增数据项信息'
        this.dialogFormVisible = true
      }
    },
    save(formName) {
      this.loading = true
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.form.id !== null && undefined !== this.form.id) {
            updateObject(this.form).then(res => {
              this.$message.success('更新成功！')
              this.close()
            }).catch(() => {
              this.loading = false
              this.$message.error('更新失败！')
            })
          } else {
            saveObject(this.form).then(res => {
              this.$message.success('保存成功！')
              this.close()
              this.loading = false
            }).catch(() => {
              this.loading = false
              this.$message.error('保存失败！')
            })
          }
        } else {
          this.loading = false
          console.log('error submit!!')
          return false
        }
      })
    },
    close() {
      this.loading = false
      Object.assign(this.$data.form, this.$options.data.call(this).form)
      this.dialogFormVisible = false
      this.$emit('fetch-data')
    },
    initDataSource() {
      queryAllLayz().then(res => {
        const { data } = res
        this.dataSources = data.data
      }).catch(() => {
        console.log('初始化数据源信息失败！')
      })
    },
    changeCron(val) {
      this.form.corn = val
    }
  }
}
</script>

<style scoped>

</style>
